package com.coffee.sku.service.inquire.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.service.platform.bean.PlatformBaseDeploy;
import com.coffee.admin.service.platform.dao.PlatformBaseDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.DateUtil;
import com.coffee.core.util.StringUtil;
import com.coffee.order.service.sku.bean.SkuOrderDetail;
import com.coffee.sku.api.inquire.query.InquireSkuPriceTrendQuery;
import com.coffee.sku.api.inquire.result.InquireSkuPriceTrendResult;
import com.coffee.sku.api.inquire.service.InquireSkuPriceTrendService;
import com.coffee.sku.service.inquire.bean.InquireSkuPriceTrend;
import com.coffee.sku.service.inquire.dao.InquireSkuPriceTrendDao;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class InquireSkuPriceTrendServiceImpl implements InquireSkuPriceTrendService {
    private static final Logger logger = LoggerFactory.getLogger(InquireSkuPriceTrendServiceImpl.class);
    @Resource
    private InquireSkuPriceTrendDao inquireSkuPriceTrendDao;
    @Resource
    private PlatformBaseDeployDao platformBaseDeployDao;

    @Override
    public Page<InquireSkuPriceTrendResult> getPager(InquireSkuPriceTrendQuery param, PagerInfo pr) {
        Page<InquireSkuPriceTrendResult> pager = new Page<InquireSkuPriceTrendResult>(pr.getStart(), pr.getPageSize());
        Integer count = inquireSkuPriceTrendDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, InquireSkuPriceTrendResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(InquireSkuPriceTrendQuery param) {
        Integer count = inquireSkuPriceTrendDao.count(param);
        return count;
    }

    @Override
    public InquireSkuPriceTrendResult getDetail(Integer id) {
        InquireSkuPriceTrend item = inquireSkuPriceTrendDao.findById(id);
        if (item == null) {
            return null;
        }
        InquireSkuPriceTrendResult result = new InquireSkuPriceTrendResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<InquireSkuPriceTrendResult> getList(InquireSkuPriceTrendQuery param, PagerInfo pr) {
        List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, InquireSkuPriceTrendResult.class);
    }

    @Override
    public List<InquireSkuPriceTrendResult> getList(InquireSkuPriceTrendQuery param) {
        List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, InquireSkuPriceTrendResult.class);
    }

    @Override
    public List<InquireSkuPriceTrendResult> getList() {
        InquireSkuPriceTrendQuery param = new InquireSkuPriceTrendQuery();
        List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, InquireSkuPriceTrendResult.class);
    }


    @Override
    public void delete(Integer id, Integer supplierId) {
        inquireSkuPriceTrendDao.deleteById(id, supplierId);
    }

    @Override
    public void addOrUpdate(InquireSkuPriceTrendResult result) throws BusinessException {
        InquireSkuPriceTrendQuery param = new InquireSkuPriceTrendQuery();
        param.setSpecificationsId(result.getSpecificationsId());
        param.setHappenTime(result.getHappenTime());
        param.setName(result.getName());
        InquireSkuPriceTrend inquireSkuPriceTrendDaoFirst = inquireSkuPriceTrendDao.findFirst(param);
        ValidateBusinessException.assertNull(inquireSkuPriceTrendDaoFirst, ExceptionEnum.ERROR_1838);
        InquireSkuPriceTrend item = new InquireSkuPriceTrend();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            inquireSkuPriceTrendDao.insert(item);
        } else {
            InquireSkuPriceTrend tmp = inquireSkuPriceTrendDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            inquireSkuPriceTrendDao.updateById(item);
        }
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.chageTime(1663430400000l));
    }

    @Override
    public Map<Integer, InquireSkuPriceTrendResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, InquireSkuPriceTrendResult> map = new HashMap<Integer, InquireSkuPriceTrendResult>();
        List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.findByIds(StringUtil.setToList(idSet));
        List<InquireSkuPriceTrendResult> resultList = DTOUtils.convertList(list, InquireSkuPriceTrendResult.class);
        for (InquireSkuPriceTrendResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, InquireSkuPriceTrendResult> getItemMap() {
        Map<Integer, InquireSkuPriceTrendResult> map = new HashMap<Integer, InquireSkuPriceTrendResult>();
        InquireSkuPriceTrendQuery param = new InquireSkuPriceTrendQuery();
        List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.page(param, 0, Integer.MAX_VALUE);
        List<InquireSkuPriceTrendResult> resultList = DTOUtils.convertList(list, InquireSkuPriceTrendResult.class);
        for (InquireSkuPriceTrendResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<String, List<InquireSkuPriceTrendResult>> getMap(Integer supplierId, Date startTime, Date endTime) {
        Map<String, List<InquireSkuPriceTrendResult>> map = new HashMap<String, List<InquireSkuPriceTrendResult>>();
        InquireSkuPriceTrendQuery param = new InquireSkuPriceTrendQuery();
        param.setSupplierId(supplierId);
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        List<InquireSkuPriceTrend> list = inquireSkuPriceTrendDao.agvPrice(param);
        List<InquireSkuPriceTrendResult> resultList = DTOUtils.convertList(list, InquireSkuPriceTrendResult.class);
        if (resultList.size() > 0) {
            Map<Integer, List<InquireSkuPriceTrendResult>> listMap = resultList.stream()
                    .filter(item -> item.getSpecificationsId() != null).sorted(Comparator.comparing(InquireSkuPriceTrendResult::getHappenTime).reversed()).collect(Collectors.toList()).stream().collect(Collectors.groupingBy(InquireSkuPriceTrendResult::getSpecificationsId));
            for (Map.Entry<Integer, List<InquireSkuPriceTrendResult>> entry : listMap.entrySet()) {
                PlatformBaseDeploy platformBaseDeploy = platformBaseDeployDao.findById(entry.getKey());
                if (platformBaseDeploy != null) {
                    map.put(platformBaseDeploy.getName(), entry.getValue());
                }
            }
        }
        return map;
    }


}
